#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 12-Nov-09 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

using Thn;
#endregion

namespace Thn.Interface.Vcl
{
    /// <summary>
    /// This control is used for indicating a lenghthy operation is underway.
    /// </summary>
    [ToolboxItem(true), ToolboxBitmap(typeof(ResourceBeacon), "Thn.Interface.Vcl.Resources.Icons.WaitControl.bmp")]
#if DEBUG
    public
#else
    public
#endif
 class WaitControl : ControlBase
    {
        #region Fields
        object mSyncRoot = new object();
        System.Windows.Forms.Timer mTimer = null;
        #endregion

        #region Properties

        #region Active
        private volatile bool mActive = false;
        /// <summary>
        /// Gets/Sets whether wait mode is started.
        /// </summary>
        [Browsable(true), DefaultValue(false), Category("Appearance"), Description("Whether wait mode is started")]
        public bool Active
        {
            get { return mActive; }
            set
            {
                lock (mSyncRoot)
                {
                    if (mActive != value)
                    {
                        mActive = value;
                        if (mActive) DoStart();
                        else DoStop();
                    }
                    #region Defensive tracing
                    else
                    {
                        //same value, do nothing
                    }
                    #endregion
                }
            }
        }
        #endregion

        #region Interval
        private int mInterval = 80;
        /// <summary>
        /// Gets/Sets the internal in milliseconds for animination update.
        /// </summary>
        [Browsable(true), DefaultValue(80), Category("Appearance"), Description("Interval in milliseconds for animation update")]
        public int Interval
        {
            get { return mInterval; }
            set { mInterval = value; }
        }
        #endregion

        #region Render Context
        private WaitControlRenderContext mRenderContext = new WaitControlRenderContext();
        /// <summary>
        /// Gets reference to current renderation context.
        /// </summary>
        protected WaitControlRenderContext RenderContext
        {
            get { return mRenderContext; }
        }
        #endregion

        #endregion

        #region Start
        /// <summary>
        /// Start waiting mode.
        /// </summary>
        public void Start()
        {
            Active = true;
        }
        #endregion

        #region Stop
        /// <summary>
        /// Stop waiting mode.
        /// </summary>
        public void Stop()
        {
            Active = false;
        }
        #endregion

        #region Do Start
        void DoStart()
        {
            //create timer
            if (mTimer == null)
            {
                mTimer = new Timer();
                mTimer.Tick += new EventHandler(mTimer_Tick);
            }
            #region Defensive tracing
            else
            {
                //already created
            }
            #endregion

            //start timer
            if (!mTimer.Enabled)
            {
                mTimer.Interval = mInterval;
                mTimer.Start();
            }
            #region Defensive tracing
            else
            {
                //already started
            }
            #endregion
        }
        #endregion

        #region Do Stop
        void DoStop()
        {
            if (mTimer != null)
            {
                mTimer.Stop();
                mTimer = null;
            }
            #region Defensive tracing
            else
            {
                //timer is null, no need to do anything.
            }
            #endregion
        }
        #endregion

        #region Timer Tick
        void mTimer_Tick(object sender, EventArgs e)
        {
            IncreaseStep();
            Invalidate();
        }
        #endregion

        #region Increase Step
        void IncreaseStep()
        {
            mRenderContext.CurrentStep++;
            if (mRenderContext.CurrentStep > mRenderContext.Steps)
            {
                mRenderContext.CurrentStep = 0;
            }
            #region Defensive tracing
            else
            {
                //continue normally
            }
            #endregion
        }
        #endregion

        #region On Get Render Context
        /// <summary>
        /// Get reference to the renderation context which is used by <see cref="IControlRenderer"/>.
        /// </summary>
        protected override RenderContext OnGetRenderContext(Graphics canvas)
        {
            return mRenderContext;
        }
        #endregion

        #region On Calculate Layout
        /// <summary>
        /// Perform layout calculations neccessary for this control.
        /// </summary>
        protected override void OnCalculateLayout()
        {
            Padding pad = this.Padding;
            Rectangle rControl = new Rectangle(pad.Left, pad.Top, Width - pad.Right, Height - pad.Bottom);

            mRenderContext.ControlRegion = rControl;
        }
        #endregion

        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public WaitControl()
        {
            this.ForeColor = Color.DarkOrange;
        }
        #endregion
    }
}
